package com.yztc.orangeentertainment.ui.main.articlecommentall;

import android.content.Context;

import com.yztc.orangeentertainment.App;
import com.yztc.orangeentertainment.bean.articlecommentall.ArticleCommentAllBean;
import com.yztc.orangeentertainment.bean.articlecommentall.ArticleCommentAllDataBean;
import com.yztc.orangeentertainment.bean.articlecommentall.ArticleCommentAllListBean;
import com.yztc.orangeentertainment.bean.channalarticlecomment.ArticleCommentUserBean;
import com.yztc.orangeentertainment.config.ChannalUrlConfig;
import com.yztc.orangeentertainment.http.ChannalRetrofitService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by My on 2016/10/14.
 */
public class ArticleCommentAllModle implements ArticleCommentAllContract.Modle {
    @Override
    public void getArticleCommentAll(String type, Map<String, String> map, final OnLoadArticleCommentAllListener loadArticleCommentAllListener, Context context) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ChannalUrlConfig.PATH.BASE_CHANNALCOMMENT_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<ArticleCommentAllBean> call=retrofit.create(ChannalRetrofitService.class)
                .getArticleCommentAll(type,map);
        call.enqueue(new Callback<ArticleCommentAllBean>() {
            @Override
            public void onResponse(Call<ArticleCommentAllBean> call, Response<ArticleCommentAllBean> response) {
                ArticleCommentAllDataBean commentAllBean=response.body().getData();
                List<ArticleCommentAllListBean> list=new ArrayList<ArticleCommentAllListBean>();
                if(commentAllBean!=null) {
                    list = commentAllBean.getList();
                }
                int i=App.getLiteOrm().insert(list);
                for(int j=0;j<list.size();j++){
                    ArticleCommentUserBean userBean=list.get(j).getUser();
                    App.getLiteOrm().insert(userBean);
                }
                loadArticleCommentAllListener.onSuccess(list);
            }

            @Override
            public void onFailure(Call<ArticleCommentAllBean> call, Throwable t) {
                loadArticleCommentAllListener.onFailure(t.getMessage());
            }
        });
    }

    @Override
    public void getArticleCommentAllFromDb(OnLoadArticleCommentAllListener loadArticleCommentAllListener) {
        List<ArticleCommentAllListBean> allBean=App.getLiteOrm().query(ArticleCommentAllListBean
                .class);
        List<ArticleCommentUserBean> userBean=App.getLiteOrm().query(ArticleCommentUserBean.class);
        for(int i=0;i<userBean.size();i++){
            allBean.get(i).setUser(userBean.get(i));
        }
        if(allBean.size()==0){
            loadArticleCommentAllListener.onFailure("失败");
        }
        else{
            loadArticleCommentAllListener.onSuccess(allBean);
        }
    }

}